package com.torry.workflow.service.web.controller;


import com.torry.workflow.api.user.api.RoleApi;
import com.torry.workflow.api.user.entity.Role;
import com.torry.workflow.common.base.result.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/role")
public class RoleController {

    @DubboReference
    private RoleApi roleApi;


    @GetMapping("allRoles")
    public Result<List<Role>> allRoles(){
        List<Role> allRoles = roleApi.getAllRoles();
        return Result.selectSuccess(allRoles);
    }


    @PostMapping("saveRoleBaseInfo")
    public Result saveRoleBaseInfo(@RequestBody Role role){
        roleApi.saveRoleBaseInfo(role);
        return Result.saveSuccess();
    }

    @GetMapping("getRoleInfo/{id}")
    public Result<Role> getRoleInfo(@PathVariable Integer id){
        return Result.selectSuccess(roleApi.getRoleInfoById(id));
    }

    @DeleteMapping("delete/{ids}")
    public Result<Role> getRoleInfo(@PathVariable String ids){

        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        List<Integer> idList = strings.stream().map(id -> Integer.parseInt(id)).collect(Collectors.toList());
        roleApi.deleteRoleByIds(idList);
        return Result.delSuccess();
    }

    @PostMapping("saveRoleSettings")
    public Result saveRoleSettings(@RequestBody Role role) {
        roleApi.saveRoleSettings(role);
        return Result.saveSuccess();
    }
}
